import React, { useState } from "react";
import fishTokenContract from "../fishToken";
import web3 from "../web3";

const GetFishByName = () => {
    const [species, setSpecies] = useState("");
    const [fishes, setFishes] = useState([]);
    const [message, setMessage] = useState("");

    const fetchFishByName = async () => {
        try {
            const fishData = await fishTokenContract.methods.getFishByName(species).call();
            setFishes(fishData);
            setMessage("");
        } catch (error) {
            setMessage(`查询失败: ${error.message}`);
        }
    };

    return (
        <div>
            <h2>根据种类查询鱼</h2>
            <input
                type="text"
                placeholder="输入鱼的种类（留空查询所有鱼）"
                value={species}
                onChange={(e) => setSpecies(e.target.value)}
            />
            <button onClick={fetchFishByName}>查询</button>
            {message && <p>{message}</p>}
            <ul>
                {fishes.map((fish, index) => (
                    <li key={index}>
                        <p>ID: {fish.id}</p>
                        <p>种类: {fish.species}</p>
                        <p>位置: {fish.location}</p>
                        <p>温度安全: {fish.isSafe ? "是" : "否"}</p>
                    </li>
                ))}
            </ul>
        </div>
    );
};

export default GetFishByName;

